<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script src="./sdk/sdk.js"></script>
    <script>
        const tags = ["web"]
        // 初始化
        let cli = new YIMClient(gatewayURL, { token: tokens[0], tags });
        let eventcallback = (evt) => {
            log.info(`event ${evt}`)
        };
        let messagecallback = (m) => {
            log.info(m)
        }
        let offmessagecallback = (om) => {
            // 离线时的发送方用户列表
            let users = om.listUsers()
            if (users.length > 0) {
                log.info(`offline messages from users of ${users}`)
                // lazy load the first page messages from 'users[0]'
                let messages = om.loadUser(users[0], 1)
                log.info(messages)
            }
            // 离线的群列表
            let groups = om.listGroups()
            if (groups.length > 0) {
                log.info(`offline messages from groups of ${groups}`)
            }
        }
        // 2.注册事件
        let evts = [YIMEvent.Closed, YIMEvent.Reconnecting, YIMEvent.Reconnected, YIMEvent.Kickout]
        cli.register(evts, eventcallback)
        cli.onmessage(messagecallback)
        cli.onofflinemessage(offmessagecallback)
        // 3. 登录
        let { success, err } = await cli.login()
        if (!success) {
            log.error(err)
            return
        }

        // 4. 发送消息
        let { status, resp, err: err2 } = await cli.talkToUser("test2", new Content("hello"))
        if (status != Status.Success) {
            log.error(err)
            return
        }
    </script>

</body>

</html>